package com.zhihaoliang.busEncy.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper
import com.alibaba.android.vlayout.layout.StickyLayoutHelper
import com.google.android.material.tabs.TabLayout
import com.hbmcc.heshenghuo.functions.utils.DeviceUtil
import com.stx.xhb.androidx.XBanner
import com.stx.xhb.androidx.entity.BaseBannerInfo
import com.zhihaoliang.busEncy.R
import com.zhihaoliang.busEncy.TAG
import com.zhihaoliang.busEncy.databinding.FloorYwnew002ItemBinding
import com.zhihaoliang.busEncy.model.BusiRightBean
import com.zhihaoliang.busEncy.model.list.FloorPagerVo

/**
 * 轮播广告模板
 */
const val YWNEW_002 = "YWNEW-002"

/**
 * 锚点楼层
 */
const val CUNSTOM_001 = "CUNSTOM_001"

/**
 * 业务卡片模板
 */
const val YWNEW_003 = "YWNEW-003"


class BusRightAdapter<T : BaseBannerInfo> internal constructor(var data: BusiRightBean<out T>) :
    DelegateAdapter.Adapter<BusRightAdapter.ViewHolder>() {
    override fun onCreateLayoutHelper(): LayoutHelper {
        Log.e(TAG, "onCreateViewHolder ${data.fModelNum}")
        return when (data.fModelNum) {
            YWNEW_002 -> LinearLayoutHelper()
            CUNSTOM_001 -> StickyLayoutHelper()
            YWNEW_003 -> StaggeredGridLayoutHelper()
            else -> LinearLayoutHelper()
        }
    }

    override fun onViewRecycled(holder: ViewHolder) {
        if (holder.itemView is ViewPager) {
            holder.itemView.adapter = null
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(viewType, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (data.fModelNum) {
            YWNEW_002 -> {
                holder.xBanner.setPointsIsVisible(data.list.size > 2)
                holder.xBanner.layoutParams.height =
                    getBannerHeight(holder.xBanner.context, data.list.size > 2)
                holder.xBanner.setBannerData(R.layout.floor_ywnew_002_item, data.list)
                holder.xBanner.loadImage { _, model, view, _ ->
                    if (model !is FloorPagerVo) return@loadImage
                    var binding = view.getTag(R.layout.floor_ywnew_002_item)
                    if (binding == null) {
                        binding = FloorYwnew002ItemBinding.bind(view)
                        view.setTag(R.layout.floor_ywnew_002_item, binding)
                    }

                    if (binding is FloorYwnew002ItemBinding) {
                        binding.viewModel = model
                    }
                }
            }
            CUNSTOM_001 -> upDateTabAnchor(holder.tbAnchor)
        }
    }

    override fun getItemCount(): Int {
        return when (data.fModelNum) {
            YWNEW_002, CUNSTOM_001 -> 1
            YWNEW_003 -> data.list.size
            else -> 0
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when (data.fModelNum) {
            YWNEW_002 -> R.layout.item_busi_banner
            CUNSTOM_001 -> R.layout.item_busi_tab
            else -> R.layout.item_busi_banner
        }
    }

    class ViewHolder(var view: View) : RecyclerView.ViewHolder(view) {
        val xBanner = view.findViewById<XBanner>(R.id.xb_busi_banner)
        val tbAnchor = view.findViewById<TabLayout>(R.id.tb_busi_tablayout)


    }

    private fun upDateTabAnchor(tbAnchor: TabLayout) {
        for (name in data.list) {
            val tabView: View =
                LayoutInflater.from(tbAnchor.context).inflate(
                    R.layout.item_busi_anchor_tab,
                    tbAnchor,
                    false
                )
            val textView = tabView.findViewById<TextView>(R.id.tv_business_tab_text)
            textView.text = name.toString()
            val tab: TabLayout.Tab =
                tbAnchor.newTab().setCustomView(tabView).setTag(name)
            tbAnchor.addTab(tab)
        }

    }


    private fun getBannerHeight(context: Context, isShowIndicator: Boolean): Int {
        val screenWidth = DeviceUtil.getScreenWidth(context)
        val leftWidth = context.resources.getDimension(R.dimen.buis_left_width)
        val padWidth = context.resources.getDimension(R.dimen.buis_pad) * 2
        val splace = context.resources.getDimension(R.dimen.buis_space)
        var height = (screenWidth - leftWidth - padWidth - splace) * 5 / 16F

        return if (isShowIndicator) (height + padWidth*1.5 ).toInt() else height.toInt()
    }


}